package com.example.tupu;

import cn.hutool.core.util.RandomUtil;
import com.example.tupu.entity.*;
import com.example.tupu.relationship.*;
import com.example.tupu.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

@Service
public class Neo4jServiceImpl implements Neo4jService {

    static String dynastyIn = "DYNASTY_IN";
    static String townIn = "TOWN_IN";
    static String riverIn = "RIVER_IN";
    static String incidentIn = "INCIDENT_IN";
    static String positionIn = "POSITION_IN";
    static String riverend = "RIVEREND";
    static String riverfrom = "RIVERFROM";
    static String societyIn = "SOCIETY_IN";
    static String birthplaceIn = "BIRTHPLACE_IN";
    static String riverTown = "RIVER_TOWN";
    static String rivermake = "RIVERMAKE";
    static String nodeEvent = "NODE_EVENT";
    static String nodeFigure = "NODE_FIGURE";
    static String nodeRiver = "NODE_RIVER";
    static String nodetown = "NODETOWN";

    static String BIRTHPLACE = "birthplace";
    static String DYNASTY = "dynasty";
    static String OFFICE = "office";
    static String EVENT = "event";
    static String TOWN = "town";
    static String RIVER = "river";
    static String FIGURE = "figure";
    static String NODE = "node";
    static String RUIN = "ruin";

    @Autowired
    private FigureRepository figureRepository;
//    private FigureRepository figureRepository;
    @Autowired
    private BirthplaceRepository birthplaceRepository;
    @Autowired
    private DynastyRepository dynastyRepository;
    @Autowired
    private EventRepository eventRepository;
    @Autowired
    private OfficeRepository officeRepository;
    @Autowired
    private TownRepository townRepository;
    @Autowired
    private RiverwayRepository riverwayRepository;
    @Autowired
    private NodeRepository nodeRepository;
    @Autowired
    private RuinRepository ruinRepository;
    @Autowired
    private YunHeUtil yunheUtil;

    @Value("${file.dir}")
    private  String dir;



    @Override
//    @MultiTransaction
    public List<Object> findLike(String title) {
        List<Figure> figureList = figureRepository.findAllByTitleLike("*"+title+"*");
        Iterator<Figure> iterator = figureList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Figure figure = iterator.next();
            objectMap.put("title", figure.getTitle());
            objectMap.put("indexId", figure.getIndexId());
            objectMap.put("form",FIGURE);
            list.add(objectMap);
        }
        List<Event> eventList = eventRepository.findAllByTitleLike("*"+title+"*");
        Iterator<Event> eventIterator = eventList.iterator();
        while (eventIterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Event event = eventIterator.next();
            objectMap.put("title", event.getTitle());
            objectMap.put("indexId", event.getIndexId());
            objectMap.put("form",EVENT);
            list.add(objectMap);
        }
        List<Riverway> riverwayList = riverwayRepository.findAllByTitleLike("*"+title+"*");
        Iterator<Riverway> riverwayIterator = riverwayList.iterator();
        while (riverwayIterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Riverway riverway = riverwayIterator.next();
            objectMap.put("title", riverway.getTitle());
            objectMap.put("indexId", riverway.getIndexId());
            objectMap.put("form",RIVER);
            list.add(objectMap);
        }
        List<Town> townList = townRepository.findAllByTitleLike("*"+title+"*");
        Iterator<Town> townIterator = townList.iterator();
        while (townIterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Town town = townIterator.next();
            objectMap.put("title", town.getTitle());
            objectMap.put("indexId", town.getIndexId());
            objectMap.put("form",TOWN);
            list.add(objectMap);
        }
        List<Dynasty> dynastyList = dynastyRepository.findAllByTitleLike("*"+title+"*");
        Iterator<Dynasty> dynastyIterator = dynastyList.iterator();
        while (dynastyIterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Dynasty dynasty = dynastyIterator.next();
            objectMap.put("title", dynasty.getTitle());
            objectMap.put("indexId", dynasty.getIndexId());
            objectMap.put("form",DYNASTY);
            list.add(objectMap);
        }
        List<Ruin> ruinList = ruinRepository.findAllByTitleLike("*"+title+"*");
        Iterator<Ruin> ruinIterator = ruinList.iterator();
        while (ruinIterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Ruin ruin = ruinIterator.next();
            objectMap.put("title", ruin.getTitle());
            objectMap.put("indexId", ruin.getIndexId());
            objectMap.put("form",RUIN);
            list.add(objectMap);
        }
        List<Office> offices = officeRepository.findAllByTitleLike("*"+title+"*");
        Iterator<Office> officeIterator = offices.iterator();
        while (officeIterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Office office = officeIterator.next();
            objectMap.put("title", office.getTitle());
            objectMap.put("indexId", office.getIndexId());
            objectMap.put("form",OFFICE);
            list.add(objectMap);
        }
        if(list.size()>10){
            int [] ints = yunheUtil.rundomNum(10,list.size());
            List<Object> list1= new LinkedList<Object>();
            for(int i =0 ; i<10;i++) {

                list1.addAll(list.subList(ints[i]-1, ints[i]));
    //            CollectionUtils.addAll(list1, new Object[]);
    //            Collections.copy(list1, list);
    //            list1.add(list.get(ints[i]));
            }
                return list1;
        }else{
            return list;
        }


    }



    @Override
    public List<Object> findNodeAllByTitle(String title) throws IOException {
        String coin = "";
        if ("河道".equals(title) || "河".equals(title) || "运河".equals(title)) {
            title = "河道";
            coin = RIVER;
        }
        if ("人物".equals(title) || "人".equals(title) || "相关人物".equals(title)) {
            title = "人物";
            coin = FIGURE;
        }
        if ("事件".equals(title) || "事".equals(title) || "事情".equals(title)) {
            title = "事件";
            coin = EVENT;
        }
        if ("城镇".equals(title) || "城".equals(title) || "地点".equals(title)) {
            title = "城镇";
            coin = TOWN;
        }
        List<NODE> nodeFirstList = nodeRepository.findAllByTitle(title);
        Iterator<NODE> iterator = nodeFirstList.iterator();
        List<Object> list = new LinkedList<Object>();

        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            NODE nodes = iterator.next();
            objectMap.put("indexId", nodes.getIndexId());
            objectMap.put("title", nodes.getTitle());
            objectMap.put("introduced", nodes.getIntroduced());
            objectMap.put("form", NODE);
            objectMap.put("icon_0", getImages(coin, "0"));
            objectMap.put("icon_1", getImages(coin, "1"));
            objectMap.put("icon_small", getImages(coin, "2"));
            List<Object> reNodeMap = new LinkedList<>();
            List<ReNodeFigure> reNodeList = nodes.getReNodeFigureList();
            Iterator<ReNodeFigure> reNodeFigureIterator = reNodeList.iterator();
            while (reNodeFigureIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReNodeFigure reNodeFigure = reNodeFigureIterator.next();

                remap.put("type", nodeFigure);
                remap.put("title", reNodeFigure.getTitle());
                Figure figure1 = reNodeFigure.getEnd();
                Map<String, Object> objectFigure = new HashMap<>();
                objectFigure.put("indexId", figure1.getIndexId());
                objectFigure.put("title", figure1.getTitle());
                objectFigure.put("year", figure1.getYear());
                objectFigure.put("death", figure1.getDeath());
                objectFigure.put("alias", figure1.getAlias());
                objectFigure.put("tags", figure1.getTags());
                objectFigure.put("achievement", figure1.getAchievement());
                objectFigure.put("profile", figure1.getProfile());
                objectFigure.put("sourceAnnals", figure1.getSourceAnnals());
                objectFigure.put("page", figure1.getPage());
                objectFigure.put("icon_0", getImages(FIGURE, "0"));
                objectFigure.put("icon_1", getImages(FIGURE, "1"));
                objectFigure.put("icon_small", getImages(FIGURE, "2"));
                objectFigure.put("form", FIGURE);
                remap.put("end", objectFigure);
                reNodeMap.add(remap);
            }
            List<Object> reNodeMap1 = new LinkedList<>();
            List<ReNodeEvent> reNodeList1 = nodes.getReNodeEventList();
            Iterator<ReNodeEvent> reNodeEventIterator = reNodeList1.iterator();
            while (reNodeEventIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReNodeEvent reNodeEvent = reNodeEventIterator.next();
                remap.put("title", reNodeEvent.getTitle());
                remap.put("type", nodeEvent);
                Event event1 = reNodeEvent.getEnd();
                Map<String, Object> objectEvent = new HashMap<>();
                objectEvent.put("indexId", event1.getIndexId());
                objectEvent.put("title", event1.getTitle());
                objectEvent.put("icon_0", getImages(EVENT, "0"));
                objectEvent.put("icon_1", getImages(EVENT, "1"));
                objectEvent.put("icon_small", getImages(EVENT, "2"));
                objectEvent.put("form", EVENT);
                remap.put("end", objectEvent);
                reNodeMap.add(remap);
            }

            List<Object> reNodeMap2 = new LinkedList<>();
            List<ReNodeTown> reNodeList2 = nodes.getReNodeTownList();
            Iterator<ReNodeTown> reNodeTownIterator = reNodeList2.iterator();
            while (reNodeTownIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReNodeTown reNodeTown = reNodeTownIterator.next();
                remap.put("type", nodetown);
                remap.put("title", reNodeTown.getTitle());
                Town town1 = reNodeTown.getEnd();
                Map<String, Object> tmap = new HashMap<>();
                tmap.put("indexId", town1.getIndexId());
                tmap.put("title", town1.getTitle());
                tmap.put("icon_0", getImages(TOWN, "0"));
                tmap.put("icon_1", getImages(TOWN, "1"));
                tmap.put("icon_small", getImages(TOWN, "2"));
                tmap.put("form", TOWN);
                remap.put("end", tmap);
                reNodeMap.add(remap);
            }
            List<Object> reNodeMap3 = new LinkedList<>();
            List<ReNodeRiver> reNodeList3 = nodes.getReNodeRiverList();
            Iterator<ReNodeRiver> reNodeIterator = reNodeList3.iterator();
            while (reNodeIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReNodeRiver ReNodeRiver = reNodeIterator.next();
                remap.put("type", nodetown);
                remap.put("title", ReNodeRiver.getTitle());
                Riverway riverway1 = ReNodeRiver.getEnd();
                Map<String, Object> rwmap = new HashMap<>();
                rwmap.put("indexId", riverway1.getIndexId());
                rwmap.put("title", riverway1.getTitle());
                rwmap.put("icon_0", getImages(RIVER, "0"));
                rwmap.put("icon_1", getImages(RIVER, "1"));
                rwmap.put("icon_small", getImages(RIVER, "2"));
                rwmap.put("form", RIVER);
                remap.put("end", rwmap);
                reNodeMap.add(remap);
            }
            objectMap.put("List", reNodeMap);

            list.add(objectMap);
        }
        return list;
    }

    @Override
    public List<Object> findFigureAllByTitle(String title) throws Exception {
//        List<Figure> figureList = figureRepository.findAllByTitleLike("*"+title+"*");
        List<Figure> figureList = figureRepository.findAllByTitle(title);
        Iterator<Figure> iterator = figureList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Figure figure = iterator.next();
            objectMap.put("indexId", figure.getIndexId());
            objectMap.put("title", figure.getTitle());
            objectMap.put("year", figure.getYear());
            objectMap.put("death", figure.getDeath());
            objectMap.put("alias", figure.getAlias());
            objectMap.put("tags", figure.getTags());
            objectMap.put("achievement", figure.getAchievement());
            objectMap.put("introduced", figure.getProfile());
            objectMap.put("sourceAnnals", figure.getSourceAnnals());
            objectMap.put("page", figure.getPage());
            objectMap.put("form", FIGURE);
            objectMap.put("icon_0", getImages(FIGURE, "0"));
            objectMap.put("icon_1", getImages(FIGURE, "1"));
            objectMap.put("icon_small", getImages(FIGURE, "2"));
            List<Object> List = new LinkedList<>();
            List<Object> List1 = new LinkedList<>();
            List<Object> reAssociatedPersonMap = new LinkedList<>();
            List<ReAssociatedPerson> reAssociatedPersonList = figure.getReAssociatedPersonList();
            Iterator<ReAssociatedPerson> Assiterator = reAssociatedPersonList.iterator();
            while (Assiterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                ReAssociatedPerson reAssociatedPerson = Assiterator.next();
                remap.put("Relation", reAssociatedPerson.getRelation());
                remap.put("title", reAssociatedPerson.getTitle());
                remap.put("office", reAssociatedPerson.getOffice());
                remap.put("introduced", reAssociatedPerson.getIntroduce());
                remap.put("historyYear", reAssociatedPerson.getHistoryYear());
                remap.put("Adyear", reAssociatedPerson.getAdyear());
                remap.put("zhishu", reAssociatedPerson.getZhishu());
                remap.put("page", reAssociatedPerson.getPage());
                remap.put("type", societyIn);
                Figure figure2 = reAssociatedPerson.getEnd();
                Map<String, Object> objectFigure1 = new HashMap<>();
                objectFigure1.put("indexId", figure2.getIndexId());
                objectFigure1.put("title", figure2.getTitle());
                objectFigure1.put("year", figure2.getYear());
                objectFigure1.put("death", figure2.getDeath());
                objectFigure1.put("alias", figure2.getAlias());
                objectFigure1.put("tags", figure2.getTags());
                objectFigure1.put("achievement", figure2.getAchievement());
                objectFigure1.put("profile", figure2.getProfile());
                objectFigure1.put("sourceAnnals", figure2.getSourceAnnals());
                objectFigure1.put("page", figure2.getPage());
                objectFigure1.put("icon_0", getImages(FIGURE, "0"));
                objectFigure1.put("icon_1", getImages(FIGURE, "1"));
                objectFigure1.put("icon_small", getImages(FIGURE, "2"));
                objectFigure1.put("form", FIGURE);
                remap.put("end", objectFigure1);
                List.add(remap);
            }

            List<Object> reOfficeListMap = new LinkedList<>();
            List<ReOffice> reOfficeListList = figure.getReOfficeList();
            Iterator<ReOffice> ReOfficeiterator = reOfficeListList.iterator();
            while (ReOfficeiterator.hasNext()) {
                Map<String, Object> remap1 = new HashMap<String, Object>();
                Map<String, Object> remap = new HashMap<String, Object>();
                ReOffice reOffice = ReOfficeiterator.next();
                remap.put("title", reOffice.getTitle());
                remap.put("introduced", reOffice.getIntroduce());
                remap.put("page", reOffice.getPage());
                remap.put("zhishu", reOffice.getZhishu());
                remap.put("type", positionIn);
                Office office = reOffice.getEnd();
                Map<String, Object> officemap = new HashMap<>();
                officemap.put("indexId", office.getIndexId());
                officemap.put("title", office.getTitle());
                officemap.put("icon_0", getImages(OFFICE, "0"));
                officemap.put("icon_1", getImages(OFFICE, "1"));
                officemap.put("icon_small", getImages(OFFICE, "2"));
                officemap.put("form", OFFICE);
                remap.put("end", officemap);
                List.add(remap);
            }

            List<Object> reEventMap = new LinkedList<>();
            List<ReEvent> reEventList = figure.getReEventList();
            Iterator<ReEvent> reEventiterator = reEventList.iterator();
            while (reEventiterator.hasNext()) {
                Map<String, Object> remap1 = new HashMap<String, Object>();
                Map<String, Object> remap = new HashMap<String, Object>();
                ReEvent reEvent = reEventiterator.next();
                remap.put("introduced", reEvent.getIntroduced());
                remap.put("title", reEvent.getTitle());
                remap.put("historyYear", reEvent.getHistoryYear());
                remap.put("Adyear", reEvent.getAdyear());
                remap.put("city", reEvent.getCity());
                remap.put("rever", reEvent.getRever());
                remap.put("renwu", reEvent.getRenwu());
                remap.put("pingjia", reEvent.getPingjia());
                remap.put("yinyong", reEvent.getYinyong());
                remap.put("zhishu", reEvent.getZhishu());
                remap.put("page", reEvent.getPage());
                remap.put("type", incidentIn);
                Event event = reEvent.getEnd();
                Map<String, Object> emap = new HashMap<>();
                Map<String, Object> emap1 = new HashMap<>();
                emap.put("indexId", event.getIndexId());
                emap.put("title", event.getTitle());
                emap.put("icon_0", getImages(EVENT, "0"));
                emap.put("icon_1", getImages(EVENT, "1"));
                emap.put("icon_small", getImages(EVENT, "2"));
                emap.put("form", EVENT);
                emap1.putAll(emap);
                remap.put("end", emap);
                remap1.putAll(remap);
                remap1.put("end", emap1);
                List.add(remap);
                List1.add(remap1);
            }

            List<Object> reBirthplaceMap = new LinkedList<>();
            List<ReBirthplace> reBirthplaceList = figure.getReBirthplaceList();
            Iterator<ReBirthplace> reBirthplaceiterator = reBirthplaceList.iterator();
            while (reBirthplaceiterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReBirthplace rebirthplace = reBirthplaceiterator.next();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                remap.put("type", birthplaceIn);
                remap.put("title", rebirthplace.getTitle());
                Town birthplace = rebirthplace.getEnd();
                Map<String, Object> bpmap = new HashMap<>();
                bpmap.put("indexId", birthplace.getIndexId());
                bpmap.put("title", birthplace.getTitle());
                bpmap.put("icon_0", getImages(TOWN, "0"));
                bpmap.put("icon_1", getImages(TOWN, "1"));
                bpmap.put("icon_small", getImages(TOWN, "2"));
                bpmap.put("form", TOWN);
                remap.put("end", bpmap);
                List.add(remap);
            }

            List<Object> reDynastyMap = new LinkedList<>();
            List<ReDynasty> reDynastyList = figure.getReDynastyList();
            Iterator<ReDynasty> reDynastyiterator = reDynastyList.iterator();
            while (reDynastyiterator.hasNext()) {
                Map<String, Object> remap1 = new HashMap<String, Object>();
                Map<String, Object> remap = new HashMap<String, Object>();
                ReDynasty reDynasty = reDynastyiterator.next();

                remap.put("type", dynastyIn);
                remap.put("title", reDynasty.getTitle());
                Dynasty dynasty = reDynasty.getEnd();
                Map<String, Object> dynastymap = new HashMap<>();
                dynastymap.put("indexId", dynasty.getIndexId());
                dynastymap.put("title", dynasty.getTitle());
                dynastymap.put("icon_0", getImages(DYNASTY, "0"));
                dynastymap.put("icon_1", getImages(DYNASTY, "1"));
                dynastymap.put("icon_small", getImages(DYNASTY, "2"));
                dynastymap.put("form", DYNASTY);
                remap.put("end", dynastymap);
                List.add(remap);
            }

            List<Object> reNodeMap = new LinkedList<>();
            List<ReNodeFigure> reNodeList = figure.getReNodeFigureList();
            Iterator<ReNodeFigure> reNodeFigureIterator = reNodeList.iterator();
            while (reNodeFigureIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                ReNodeFigure reNodeFigure = reNodeFigureIterator.next();
                remap.put("type", nodeFigure);
                remap.put("title", reNodeFigure.getTitle());
                NODE nodeFirst = reNodeFigure.getStart();
                Map<String, Object> nodemap = new HashMap<>();
                nodemap.put("indexId", nodeFirst.getIndexId());
                nodemap.put("title", nodeFirst.getTitle());
                nodemap.put("icon_0", getImages(FIGURE, "1"));
                nodemap.put("icon_1", getImages(FIGURE, "1"));
                nodemap.put("icon_small", getImages(FIGURE, "2"));
                nodemap.put("form", NODE);
                remap.put("end", nodemap);
                List.add(remap);
            }
//            CollectionUtils.addAll(List1, new Object[List.size()]);
//            Collections.copy(List1, List);
            objectMap.put("List", List);
            objectMap.put("reEventList", List1);
            list.add(objectMap);
        }

        return list;
    }


    @Override
    public List<Object> findDynastyAllByTitle(String title) throws Exception {
        List<Dynasty> dynastyList = dynastyRepository.findAllByTitle(title);
        Iterator<Dynasty> iterator = dynastyList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Dynasty dynasty = iterator.next();
            objectMap.put("indexId", dynasty.getIndexId());
            objectMap.put("title", dynasty.getTitle());
            objectMap.put("icon_0", getImages(DYNASTY, "0"));
            objectMap.put("icon_1", getImages(DYNASTY, "1"));
            objectMap.put("icon_small", getImages(DYNASTY, "2"));
            objectMap.put("form", DYNASTY);
            List<Object> reDynastyMap = new LinkedList<>();
            List<Object> List = new LinkedList<>();
            List<ReDynasty> reDynastyList = dynasty.getReDynastyList();
            Iterator<ReDynasty> Dynastyiterator = reDynastyList.iterator();
            while (Dynastyiterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReDynasty reDynasty = Dynastyiterator.next();
                remap.put("title", reDynasty.getTitle());
                remap.put("type", dynastyIn);
                Figure figure1 = reDynasty.getStart();
                Map<String, Object> objectFigure = new HashMap<>();
                objectFigure.put("indexId", figure1.getIndexId());
                objectFigure.put("title", figure1.getTitle());
                objectFigure.put("year", figure1.getYear());
                objectFigure.put("death", figure1.getDeath());
                objectFigure.put("alias", figure1.getAlias());
                objectFigure.put("tags", figure1.getTags());
                objectFigure.put("achievement", figure1.getAchievement());
                objectFigure.put("profile", figure1.getProfile());
                objectFigure.put("sourceAnnals", figure1.getSourceAnnals());
                objectFigure.put("page", figure1.getPage());
                objectFigure.put("icon_0", getImages(FIGURE, "0"));
                objectFigure.put("icon_1", getImages(FIGURE, "1"));
                objectFigure.put("icon_small", getImages(FIGURE, "2"));
                objectFigure.put("form", FIGURE);
                remap.put("end", objectFigure);
                List.add(remap);
            }
            objectMap.put("List", List);
            list.add(objectMap);
        }
        return list;
    }

    @Override
    public List<Object> findEventAllByTitle(String title) throws Exception {
        List<Event> eventList = eventRepository.findAllByTitle(title);
        Iterator<Event> iterator = eventList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Event event = iterator.next();
            objectMap.put("indexId", event.getIndexId());
            objectMap.put("title", event.getTitle());
            objectMap.put("icon_0", getImages(EVENT, "0"));
            objectMap.put("icon_1", getImages(EVENT, "1"));
            objectMap.put("icon_small", getImages(EVENT, "2"));
            objectMap.put("form", EVENT);
            List<Object> List = new LinkedList<>();
            List<Object> reEventMap = new LinkedList<>();
            List<ReEvent> reEventList = event.getReEventList();
            Iterator<ReEvent> reEventiterator = reEventList.iterator();
            while (reEventiterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                ReEvent reEvent = reEventiterator.next();
                remap.put("introduced", reEvent.getIntroduced());
                remap.put("title", reEvent.getTitle());
                remap.put("historyYear", reEvent.getHistoryYear());
                remap.put("Adyear", reEvent.getAdyear());
                remap.put("city", reEvent.getCity());
                remap.put("rever", reEvent.getRever());
                remap.put("renwu", reEvent.getRenwu());
                remap.put("pingjia", reEvent.getPingjia());
                remap.put("yinyong", reEvent.getYinyong());
                remap.put("zhishu", reEvent.getZhishu());
                remap.put("page", reEvent.getPage());
                remap.put("type", incidentIn);
                Figure figure1 = reEvent.getStart();
                Map<String, Object> objectFigure = new HashMap<>();
                objectFigure.put("indexId", figure1.getIndexId());
                objectFigure.put("title", figure1.getTitle());
                objectFigure.put("year", figure1.getYear());
                objectFigure.put("death", figure1.getDeath());
                objectFigure.put("alias", figure1.getAlias());
                objectFigure.put("tags", figure1.getTags());
                objectFigure.put("achievement", figure1.getAchievement());
                objectFigure.put("profile", figure1.getProfile());
                objectFigure.put("sourceAnnals", figure1.getSourceAnnals());
                objectFigure.put("icon_0", getImages(FIGURE, "0"));
                objectFigure.put("icon_1", getImages(FIGURE, "1"));
                objectFigure.put("icon_small", getImages(FIGURE, "2"));
                objectFigure.put("page", figure1.getPage());
                objectFigure.put("form", FIGURE);
                Map<String, Object> objectFigure1 = new HashMap<>();
                objectFigure1.putAll(objectFigure);
                remap.put("end", objectFigure);
                remap1.putAll(remap);
                remap1.put("end", objectFigure1);
                List.add(remap);
                reEventMap.add(remap1);
            }


            List<Object> reRiverwayMap = new LinkedList<>();
            List<ReRiverway> reRiverwayList = event.getReRiverwayList();
            Iterator<ReRiverway> reRiverwayIterator = reRiverwayList.iterator();
            while (reRiverwayIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverway reRiverway = reRiverwayIterator.next();
                remap.put("title", reRiverway.getTitle());
                remap.put("type", riverIn);
                Riverway riverway = reRiverway.getEnd();
                Map<String, Object> emap = new HashMap<>();
                emap.put("indexId", riverway.getIndexId());
                emap.put("title", riverway.getTitle());
                emap.put("icon_0", getImages(RIVER, "0"));
                emap.put("icon_1", getImages(RIVER, "1"));
                emap.put("icon_small", getImages(RIVER, "2"));
                emap.put("form", RIVER);
                remap.put("end", emap);
                List.add(remap);
            }

            List<Object> reTownMap = new LinkedList<>();
            List<ReTown> reTownList = event.getReTownList();
            Iterator<ReTown> reTownIterator = reTownList.iterator();
            while (reTownIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                ReTown reTown = reTownIterator.next();
                remap.put("title", reTown.getTitle());
                remap.put("type", townIn);
                Town town = reTown.getEnd();
                Map<String, Object> tmap = new HashMap<>();
                tmap.put("indexId", town.getIndexId());
                tmap.put("title", town.getTitle());
                tmap.put("jwd", town.getJwd());
                tmap.put("icon_0", getImages(TOWN, "0"));
                tmap.put("icon_1", getImages(TOWN, "1"));
                tmap.put("icon_small", getImages(TOWN, "2"));
                tmap.put("form", TOWN);
                remap.put("end", tmap);
                List.add(remap);
            }

            List<Object> reNodeMap = new LinkedList<>();
            List<ReNodeEvent> reNodeList = event.getReNodeEventList();
            Iterator<ReNodeEvent> reNodeEventIterator = reNodeList.iterator();
            while (reNodeEventIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReNodeEvent reNodeEvent = reNodeEventIterator.next();
                remap.put("title", reNodeEvent.getTitle());
                remap.put("type", nodeEvent);
                NODE nodeFirst = reNodeEvent.getStart();
                Map<String, Object> nodemap = new HashMap<>();
                nodemap.put("indexId", nodeFirst.getIndexId());
                nodemap.put("title", nodeFirst.getTitle());
                nodemap.put("introduced", nodeFirst.getIntroduced());
                nodemap.put("introduced", nodeFirst.getIntroduced());
                nodemap.put("icon_0", getImages(EVENT, "1"));
                nodemap.put("icon_1", getImages(EVENT, "1"));
                nodemap.put("icon_small", getImages(EVENT, "2"));
                nodemap.put("form", NODE);
                remap.put("end", nodemap);
                reNodeMap.add(remap);
            }
            objectMap.put("List", List);
            objectMap.put("reEventList", reEventMap);
            list.add(objectMap);
        }
        return list;

    }

    @Override
    public List<Object> findOfficeAllByTitle(String title) throws Exception {
        List<Office> officeList = officeRepository.findAllByTitle(title);
        Iterator<Office> iterator = officeList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Office office = iterator.next();
            objectMap.put("indexId", office.getIndexId());
            objectMap.put("title", office.getTitle());
            objectMap.put("icon_0", getImages(OFFICE, "0"));
            objectMap.put("icon_1", getImages(OFFICE, "1"));
            objectMap.put("icon_small", getImages(OFFICE, "2"));
            objectMap.put("form", OFFICE);
            List<Object> List = new LinkedList<>();

            List<ReOffice> reOfficeList = office.getReOfficeList();
            Iterator<ReOffice> reOfficeIterator = reOfficeList.iterator();
            while (reOfficeIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReOffice reOffice = reOfficeIterator.next();
                remap.put("title", reOffice.getTitle());
                remap.put("introduced", reOffice.getIntroduce());
                remap.put("page", reOffice.getPage());
                remap.put("zhishu", reOffice.getZhishu());
                remap.put("type", positionIn);
                Figure figure2 = reOffice.getStart();
                Map<String, Object> objectFigure = new HashMap<>();
                objectFigure.put("indexId", figure2.getIndexId());
                objectFigure.put("title", figure2.getTitle());
                objectFigure.put("year", figure2.getYear());
                objectFigure.put("death", figure2.getDeath());
                objectFigure.put("alias", figure2.getAlias());
                objectFigure.put("tags", figure2.getTags());
                objectFigure.put("achievement", figure2.getAchievement());
                objectFigure.put("profile", figure2.getProfile());
                objectFigure.put("sourceAnnals", figure2.getSourceAnnals());
                objectFigure.put("page", figure2.getPage());
                objectFigure.put("icon_0", getImages(FIGURE, "0"));
                objectFigure.put("icon_1", getImages(FIGURE, "1"));
                objectFigure.put("icon_small", getImages(FIGURE, "2"));
                objectFigure.put("form", FIGURE);
                remap.put("end", objectFigure);
                List.add(remap);
            }
            objectMap.put("List", List);

            list.add(objectMap);
        }
        return list;
    }

    @Override
    public List<Object> findBirthplaceAllByTitle(String title) throws Exception {
        List<ReBirthplace> birthplaceList = birthplaceRepository.findAllByTitle(title);
        Iterator<ReBirthplace> iterator = birthplaceList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            ReBirthplace birthplace = iterator.next();
            objectMap.put("title", birthplace.getTitle());
            objectMap.put("icon_0", getImages(TOWN, "0"));
            objectMap.put("icon_1", getImages(TOWN, "1"));
            objectMap.put("icon_small", getImages(TOWN, "2"));
            objectMap.put("form", TOWN);
            list.add(objectMap);
        }
        return list;
    }

    @Override
    public List<Object> findTownAllByTitle(String title) throws Exception {
        List<Town> townList = townRepository.findAllByTitle(title);
        Iterator<Town> iterator = townList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Town town = iterator.next();
            objectMap.put("indexId", town.getIndexId());
            if (town.getIntroduce() != null) {
                objectMap.put("introduced", town.getIntroduce());
            } else {
                objectMap.put("introduced", "");
            }
            objectMap.put("title", town.getTitle());
            objectMap.put("icon_0", getImages(TOWN, "0"));
            objectMap.put("icon_1", getImages(TOWN, "1"));
            objectMap.put("icon_small", getImages(TOWN, "2"));
            objectMap.put("form", TOWN);
            List<Object> List = new LinkedList<>();
            List<Object> List1 = new LinkedList<>();
            List<ReTown> reTownList = town.getReTownList();
            Iterator<ReTown> reTownIterator = reTownList.iterator();
            while (reTownIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                ReTown reTown = reTownIterator.next();

                remap.put("type", townIn);
                remap.put("title", reTown.getTitle());
                Event event = reTown.getStart();
                Map<String, Object> objectEvent = new HashMap<>();
                objectEvent.put("indexId", event.getIndexId());
                objectEvent.put("title", event.getTitle());
                objectEvent.put("icon_0", getImages(EVENT, "0"));
                objectEvent.put("icon_1", getImages(EVENT, "1"));
                objectEvent.put("icon_small", getImages(EVENT, "2"));
                objectEvent.put("form", EVENT);
                Map<String, Object> objectEvent1 = new HashMap<>();
                objectEvent1.putAll(objectEvent);
                remap.put("end", objectEvent);
                remap1.putAll(remap);
                remap1.put("end", objectEvent1);
                List.add(remap);
                List1.add(remap1);
            }
            List<ReBirthplace> reBirthplaceList = town.getReBirthplaceList();
            Iterator<ReBirthplace> reBirthplaceIterator = reBirthplaceList.iterator();
            while (reBirthplaceIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReBirthplace rebirthplace = reBirthplaceIterator.next();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                remap.put("type", birthplaceIn);
                remap.put("title", rebirthplace.getTitle());
                Figure figure1 = rebirthplace.getStart();
                Map<String, Object> objectFigure = new HashMap<>();
                objectFigure.put("indexId", figure1.getIndexId());
                objectFigure.put("title", figure1.getTitle());
                objectFigure.put("year", figure1.getYear());
                objectFigure.put("death", figure1.getDeath());
                objectFigure.put("alias", figure1.getAlias());
                objectFigure.put("tags", figure1.getTags());
                objectFigure.put("achievement", figure1.getAchievement());
                objectFigure.put("profile", figure1.getProfile());
                objectFigure.put("sourceAnnals", figure1.getSourceAnnals());
                objectFigure.put("page", figure1.getPage());
                objectFigure.put("icon_0", getImages(FIGURE, "0"));
                objectFigure.put("icon_1", getImages(FIGURE, "1"));
                objectFigure.put("icon_small", getImages(FIGURE, "2"));
                objectFigure.put("form", FIGURE);
                remap.put("end", objectFigure);
                List.add(remap);
            }

            List<ReNodeTown> reNodeList = town.getReNodeTownList();
            Iterator<ReNodeTown> reNodeTownIterator = reNodeList.iterator();
            while (reNodeTownIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReNodeTown reNodeTown = reNodeTownIterator.next();

                remap.put("type", nodetown);
                remap.put("title", reNodeTown.getTitle());
                NODE nodeFirst = reNodeTown.getStart();
                Map<String, Object> objectNode = new HashMap<>();
                objectNode.put("indexId", nodeFirst.getIndexId());
                objectNode.put("introduced", nodeFirst.getIntroduced());
                objectNode.put("title", nodeFirst.getTitle());
                objectNode.put("icon_0", getImages(TOWN, "1"));
                objectNode.put("icon_1", getImages(TOWN, "1"));
                objectNode.put("icon_small", getImages(TOWN, "2"));
                objectNode.put("form", NODE);
                remap.put("end", objectNode);
                List.add(remap);
            }

            List<ReRiverFrom> reRiverFromList = town.getReRiverFromList();
            Iterator<ReRiverFrom> reRiverFromIterator = reRiverFromList.iterator();
            while (reRiverFromIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverFrom reRiverFrom = reRiverFromIterator.next();

                remap.put("type", riverfrom);
                remap.put("title", reRiverFrom.getTitle());
                Riverway riverway1 = reRiverFrom.getStart();
                Map<String, Object> objectRiver = new HashMap<>();
                objectRiver.put("introduced", riverway1.getIntroduce());
                objectRiver.put("indexId", riverway1.getIndexId());
                objectRiver.put("title", riverway1.getTitle());
                objectRiver.put("icon_0", getImages(RIVER, "0"));
                objectRiver.put("icon_1", getImages(RIVER, "1"));
                objectRiver.put("icon_small", getImages(RIVER, "2"));
                objectRiver.put("form", RIVER);
                remap.put("end", objectRiver);
                List.add(remap);
            }

            List<ReRiverEnd> reRiverEndList = town.getReRiverEndList();
            Iterator<ReRiverEnd> reRiverEndIterator = reRiverEndList.iterator();
            while (reRiverEndIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverEnd reRiverEnd = reRiverEndIterator.next();
                remap.put("type", riverend);
                remap.put("title", reRiverEnd.getTitle());
                Riverway riverway1 = reRiverEnd.getStart();
                Map<String, Object> objectRiver = new HashMap<>();
                objectRiver.put("introduced", riverway1.getIntroduce());
                objectRiver.put("indexId", riverway1.getIndexId());
                objectRiver.put("title", riverway1.getTitle());
                objectRiver.put("icon_0", getImages(RIVER, "0"));
                objectRiver.put("icon_1", getImages(RIVER, "1"));
                objectRiver.put("icon_small", getImages(RIVER, "2"));
                objectRiver.put("form", RIVER);
                remap.put("end", objectRiver);
                List.add(remap);
            }

            List<ReRiverTown> reRiverTownList = town.getReRiverTownList();
            Iterator<ReRiverTown> reRiverTownIterator = reRiverTownList.iterator();
            while (reRiverTownIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverTown reRiverTown = reRiverTownIterator.next();

                remap.put("type", riverTown);
                remap.put("title", reRiverTown.getTitle());
                Riverway riverway1 = reRiverTown.getStart();
                Map<String, Object> objectRiver = new HashMap<>();
                objectRiver.put("introduced", riverway1.getIntroduce());
                objectRiver.put("indexId", riverway1.getIndexId());
                objectRiver.put("title", riverway1.getTitle());
                objectRiver.put("icon_0", getImages(RIVER, "0"));
                objectRiver.put("icon_1", getImages(RIVER, "1"));
                objectRiver.put("icon_small", getImages(RIVER, "2"));
                objectRiver.put("form", RIVER);
                remap.put("end", objectRiver);
                List.add(remap);
            }
            objectMap.put("List", List);
            //objectMap.put("reEventList", List1);
            list.add(objectMap);
        }
        return list;
    }

    @Override
    public List<Object> findRuinAllByTitle(String title) throws IOException {
        List<Ruin> townList = ruinRepository.findAllByTitle(title);
        Iterator<Ruin> iterator = townList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Ruin ruin = iterator.next();
            objectMap.put("indexId", ruin.getIndexId());
            objectMap.put("introduced", ruin.getIntroduce());
            objectMap.put("title", ruin.getTitle());
            objectMap.put("icon_0", getImages(RUIN, "0"));
            objectMap.put("icon_1", getImages(RUIN, "1"));
            objectMap.put("icon_small", getImages(RUIN, "2"));
            objectMap.put("form", RUIN);
            list.add(objectMap);
        }


        return list;
    }

    @Override
    public List<Object> findRiverwayAllByTitle(String title) throws Exception {
        List<Riverway> townList = riverwayRepository.findAllByTitle(title);
        Iterator<Riverway> iterator = townList.iterator();
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Riverway riverway = iterator.next();
            objectMap.put("indexId", riverway.getIndexId());
            objectMap.put("introduced", riverway.getIntroduce());
            objectMap.put("title", riverway.getTitle());
            objectMap.put("icon_0", getImages(RIVER, "0"));
            objectMap.put("icon_1", getImages(RIVER, "1"));
            objectMap.put("icon_small", getImages(RIVER, "2"));
            objectMap.put("form", RIVER);
            List<Object> List = new LinkedList<>();
            List<Object> List1 = new LinkedList<>();
            List<ReRiverway> reRiverwayList = riverway.getReRiverwayList();
            Iterator<ReRiverway> reRiverwayIterator = reRiverwayList.iterator();
            while (reRiverwayIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverway reRiverway = reRiverwayIterator.next();
                Map<String, Object> remap1 = new HashMap<String, Object>();
                remap.put("type", riverIn);
                remap.put("title", reRiverway.getTitle());
                Event event = reRiverway.getStart();
                Map<String, Object> objectEvent = new HashMap<>();
                Map<String, Object> objectEvent1 = new HashMap<>();
                objectEvent.put("indexId", event.getIndexId());
                objectEvent.put("title", event.getTitle());
                objectEvent.put("icon_0", getImages(EVENT, "0"));
                objectEvent.put("icon_1", getImages(EVENT, "1"));
                objectEvent.put("icon_small", getImages(EVENT, "2"));
                objectEvent.put("form", EVENT);
                //objectEvent1.putAll(objectEvent);
                remap.put("end", objectEvent);
                //remap1.putAll(remap);
                // remap1.put("end", objectEvent1);
                List.add(remap);
                // List1.add(remap1);
            }

            List<ReNodeRiver> reNodeList = riverway.getReNodeRiverList();
            Iterator<ReNodeRiver> reNodeIterator = reNodeList.iterator();
            while (reNodeIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReNodeRiver reNodeRiver = reNodeIterator.next();

                remap.put("type", nodeRiver);
                remap.put("title", reNodeRiver.getTitle());
                NODE nodeFirst = reNodeRiver.getStart();
                Map<String, Object> objectEvent = new HashMap<>();
                objectEvent.put("indexId", nodeFirst.getIndexId());
                objectEvent.put("title", nodeFirst.getTitle());
                objectEvent.put("introduced", nodeFirst.getIntroduced());
                objectEvent.put("icon_0", getImages(RIVER, "1"));
                objectEvent.put("icon_1", getImages(RIVER, "1"));
                objectEvent.put("icon_small", getImages(RIVER, "2"));
                objectEvent.put("form", NODE);
                remap.put("end", objectEvent);
                List.add(remap);
            }

            List<ReRiverFrom> reRiverFromList = riverway.getReRiverFromList();
            Iterator<ReRiverFrom> reRiverFromIterator = reRiverFromList.iterator();
            while (reRiverFromIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverFrom reRiverFrom = reRiverFromIterator.next();
                remap.put("type", riverfrom);
                remap.put("title", reRiverFrom.getTitle());
                Town town = reRiverFrom.getEnd();
                Map<String, Object> rwmap = new HashMap<>();
                rwmap.put("indexId", town.getIndexId());
                rwmap.put("title", town.getTitle());
                rwmap.put("introduced", town.getIntroduce());
                rwmap.put("jwd", town.getJwd());
                rwmap.put("icon_0", getImages(TOWN, "0"));
                rwmap.put("icon_1", getImages(TOWN, "1"));
                rwmap.put("icon_small", getImages(TOWN, "2"));
                rwmap.put("form", TOWN);
                remap.put("end", rwmap);
                List.add(remap);
            }

            List<ReRiverEnd> reRiverEndList = riverway.getReRiverEndList();
            Iterator<ReRiverEnd> reRiverEndIterator = reRiverEndList.iterator();
            while (reRiverEndIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverEnd reRiverEnd = reRiverEndIterator.next();

                remap.put("type", riverend);
                remap.put("title", reRiverEnd.getTitle());
                Town town = reRiverEnd.getEnd();
                Map<String, Object> tmap = new HashMap<>();
                tmap.put("indexId", town.getIndexId());
                tmap.put("title", town.getTitle());
                tmap.put("introduced", town.getIntroduce());
                tmap.put("jwd", town.getJwd());
                tmap.put("icon_0", getImages(TOWN, "0"));
                tmap.put("icon_1", getImages(TOWN, "1"));
                tmap.put("icon_small", getImages(TOWN, "2"));
                tmap.put("form", TOWN);
                remap.put("end", tmap);
                List.add(remap);
            }

            List<ReRiverTown> reRiverTownList = riverway.getReRiverTownList();
            Iterator<ReRiverTown> reRiverTownIterator = reRiverTownList.iterator();
            while (reRiverTownIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverTown reRiverTown = reRiverTownIterator.next();
                remap.put("type", riverTown);
                remap.put("title", reRiverTown.getTitle());
                Town town = reRiverTown.getEnd();
                Map<String, Object> tmap = new HashMap<>();
                tmap.put("indexId", town.getIndexId());
                tmap.put("title", town.getTitle());
                tmap.put("introduced", town.getIntroduce());
                tmap.put("jwd", town.getJwd());
                tmap.put("icon_0", getImages(TOWN, "0"));
                tmap.put("icon_1", getImages(TOWN, "1"));
                tmap.put("icon_small", getImages(TOWN, "2"));
                tmap.put("form", TOWN);
                remap.put("end", tmap);
                List.add(remap);
            }

            List<ReRiverMake> reRiverMakeList1 = riverway.getReRiverMakeList();
            Iterator<ReRiverMake> reRiverMakeIterator = reRiverMakeList1.iterator();
            while (reRiverMakeIterator.hasNext()) {
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverMake reRiverMake = reRiverMakeIterator.next();
                remap.put("type", rivermake);
                remap.put("title", reRiverMake.getTitle());
                Riverway riverMakeEnd = reRiverMake.getEnd();
                Map<String, Object> objectRiverEnd = new HashMap<>();
                objectRiverEnd.put("introduced", riverMakeEnd.getIntroduce());
                objectRiverEnd.put("indexId", riverMakeEnd.getIndexId());
                objectRiverEnd.put("title", riverMakeEnd.getTitle());
                objectRiverEnd.put("icon_0", getImages(RIVER, "0"));
                objectRiverEnd.put("icon_1", getImages(RIVER, "1"));
                objectRiverEnd.put("icon_small", getImages(RIVER, "2"));
                objectRiverEnd.put("form", RIVER);
                remap.put("end", objectRiverEnd);
                List.add(remap);
            }
            objectMap.put("List", List);
            //objectMap.put("reEventList", List1);
            list.add(objectMap);
        }
        return list;
    }


    @Override
    public List<Figure> getFigure(String title, int limit) {
        List<Figure> ameaings1 = figureRepository.getFigure(title, limit);
        return ameaings1;
    }


    @Override
    public List<Object> findRiverwayJwdByTitle(String title) throws Exception {
        Iterator<Riverway> iterator;
        if ("All".equals(title)) {
            Iterable<Riverway> townList = riverwayRepository.findAll();
            iterator = townList.iterator();
        } else if ("京杭大运河".equals(title)) {
            List<Riverway> townList = riverwayRepository.findAllByTitle("通惠河");
            townList.addAll(riverwayRepository.findAllByTitle("南运河"));
            townList.addAll(riverwayRepository.findAllByTitle("会通河"));
            townList.addAll(riverwayRepository.findAllByTitle("中运河"));
            townList.addAll(riverwayRepository.findAllByTitle("淮扬运河"));
            townList.addAll(riverwayRepository.findAllByTitle("江南运河"));
            iterator = townList.iterator();
        } else if ("隋唐大运河".equals(title)){
            List<Riverway> townList = riverwayRepository.findAllByTitle("永济渠");
            townList.addAll(riverwayRepository.findAllByTitle("通济渠"));
            iterator = townList.iterator();
        }else{
            List<Riverway> townList = riverwayRepository.findAllByTitle(title);
            iterator = townList.iterator();
        }
        List<Object> list = new LinkedList<Object>();
        while (iterator.hasNext()) {

            Riverway riverway = iterator.next();


            List<ReRiverFrom> reRiverFromList = riverway.getReRiverFromList();
            Iterator<ReRiverFrom> reRiverFromIterator = reRiverFromList.iterator();
            while (reRiverFromIterator.hasNext()) {
                Map<String, Object> objectMap = new HashMap<String, Object>();
                List<Object> List = new LinkedList<>();
                ReRiverFrom reRiverFrom = reRiverFromIterator.next();
                Town town = reRiverFrom.getEnd();
                Map<String, Object> rwmap = new HashMap<>();
                rwmap.put("town", riverway.getTitle() + reRiverFrom.getTitle());
                rwmap.put("indexId", town.getIndexId());
                rwmap.put("title", town.getTitle());
                rwmap.put("introduced", town.getIntroduce());
                rwmap.put("jwd", town.getJwd());
                rwmap.put("icon_small", getImages(TOWN, "2"));
                rwmap.put("form",TOWN);
                List.add(rwmap);
                objectMap.put("List", List);
                objectMap.put("indexId", riverway.getIndexId());
                objectMap.put("town", riverway.getTitle() + reRiverFrom.getTitle()+town.getTitle());
                list.add(objectMap);
            }
            List<ReRiverEnd> reRiverEndList = riverway.getReRiverEndList();
            Iterator<ReRiverEnd> reRiverEndIterator = reRiverEndList.iterator();
            while (reRiverEndIterator.hasNext()) {
                Map<String, Object> objectMap = new HashMap<String, Object>();
                List<Object> List = new LinkedList<>();
                Map<String, Object> remap = new HashMap<String, Object>();
                ReRiverEnd reRiverEnd = reRiverEndIterator.next();
                Town town = reRiverEnd.getEnd();
                Map<String, Object> tmap = new HashMap<>();
                tmap.put("town", riverway.getTitle() + reRiverEnd.getTitle());
                tmap.put("indexId", town.getIndexId());
                tmap.put("title", town.getTitle());
                tmap.put("introduced", town.getIntroduce());
                tmap.put("jwd", town.getJwd());
                tmap.put("icon_small", getImages(TOWN, "2"));
                tmap.put("form", TOWN);
                List.add(tmap);
                objectMap.put("List", List);
                objectMap.put("indexId", riverway.getIndexId());
                objectMap.put("town", riverway.getTitle() + reRiverEnd.getTitle()+town.getTitle());
                list.add(objectMap);
            }
            List<ReRiverTown> reRiverTownList = riverway.getReRiverTownList();
            Iterator<ReRiverTown> reRiverTownIterator = reRiverTownList.iterator();
            while (reRiverTownIterator.hasNext()) {
                Map<String, Object> objectMap = new HashMap<String, Object>();
                List<Object> List = new LinkedList<>();
                ReRiverTown reRiverTown = reRiverTownIterator.next();
                Town town = reRiverTown.getEnd();
                Map<String, Object> tmap = new HashMap<>();
                tmap.put("town", riverway.getTitle() + reRiverTown.getTitle()+town.getTitle());
                tmap.put("indexId", town.getIndexId());
                tmap.put("title", town.getTitle());
                tmap.put("introduced", town.getIntroduce());
                tmap.put("jwd", town.getJwd());
                tmap.put("icon_small", getImages(TOWN, "2"));
                tmap.put("form", TOWN);
                List.add(tmap);
                objectMap.put("List", List);
                objectMap.put("indexId", riverway.getIndexId());
                objectMap.put("town", riverway.getTitle() + reRiverTown.getTitle()+town.getTitle());
                list.add(objectMap);
            }
            List<ReRiverMake> reRiverMakeList1 = riverway.getReRiverMakeList();
            Iterator<ReRiverMake> reRiverMakeIterator = reRiverMakeList1.iterator();
            while (reRiverMakeIterator.hasNext()) {
                ReRiverMake reRiverMake = reRiverMakeIterator.next();
                Riverway riverMakeEnd = reRiverMake.getEnd();
                List<Riverway> list1 = riverwayRepository.findAllByTitle(riverMakeEnd.getTitle());
                Iterator<Riverway> iterator1 = list1.iterator();
                while (iterator1.hasNext()) {
                    Riverway riverway1 = iterator1.next();
                    List<ReRiverFrom> reRiverFromList1 = riverway1.getReRiverFromList();
                    Iterator<ReRiverFrom> reRiverFromIterator1 = reRiverFromList1.iterator();
                    while (reRiverFromIterator1.hasNext()) {
                        Map<String, Object> objectMap = new HashMap<String, Object>();
                        List<Object> List = new LinkedList<>();
                        ReRiverFrom reRiverFrom = reRiverFromIterator1.next();
                        Town town = reRiverFrom.getEnd();
                        Riverway riverway2 = reRiverFrom.getStart();
                        Map<String, Object> map = new HashMap<>();
                        map.put("town", riverway1.getTitle() + reRiverFrom.getTitle());
                        map.put("indexId", town.getIndexId());
                        map.put("title", town.getTitle());
                        map.put("introduced", town.getIntroduce());
                        map.put("jwd", town.getJwd());
                        map.put("icon_small", getImages(TOWN, "2"));
                        map.put("form", TOWN);
                        List.add(map);
                        objectMap.put("List", List);
                        objectMap.put("indexId", riverway.getIndexId());
                        objectMap.put("town", riverway.getTitle()+reRiverMake.getTitle()+riverway2.getTitle()+ reRiverFrom.getTitle()+town.getTitle());
                        list.add(objectMap);
                    }
                    List<ReRiverEnd> reRiverEndList1 = riverway1.getReRiverEndList();
                    Iterator<ReRiverEnd> reRiverEndIterator1 = reRiverEndList1.iterator();
                    while (reRiverEndIterator1.hasNext()) {
                        Map<String, Object> objectMap = new HashMap<String, Object>();
                        List<Object> List = new LinkedList<>();
                        Map<String, Object> map = new HashMap<>();
                        ReRiverEnd reRiverEnd = reRiverEndIterator1.next();
                        Town town = reRiverEnd.getEnd();
                        Riverway riverway2 = reRiverEnd.getStart();
                        map.put("town", riverway.getTitle() + reRiverEnd.getTitle());
                        map.put("indexId", town.getIndexId());
                        map.put("title", town.getTitle());
                        map.put("introduced", town.getIntroduce());
                        map.put("jwd", town.getJwd());
                        map.put("icon_small", getImages(TOWN, "2"));
                        map.put("form", TOWN);
                        List.add(map);
                        objectMap.put("List", List);
                        objectMap.put("indexId", riverway.getIndexId());
                        objectMap.put("town", riverway.getTitle() +reRiverMake.getTitle()+riverway2.getTitle()+ reRiverEnd.getTitle()+town.getTitle());
                        list.add(objectMap);
                    }
                }
            }
//            objectMap.put("List", List);
//            list.add(objectMap);
        }
        return list;
    }


    @Override
    public List findTownJwdByTitle(String title) throws Exception {
        List<Town> eventList = new LinkedList<>();
        Iterator<Town> iterator;
        if("".equals(title)||title.isEmpty()){
            Iterable<Town> nodeFirstList = townRepository.findAll();
            iterator = nodeFirstList.iterator();
        }else{
            eventList = townRepository.findAllByTitle(title);
            iterator = eventList.iterator();
        }

        List mapList = new ArrayList();
        while (iterator.hasNext()) {
            List<Map<String, Object>> mapList1 = new ArrayList<Map<String, Object>>();
            Map<String, Object> Listmap = new HashMap<>();
            Map<String, Object> map = new HashMap<>();
            List<Map> ll = new ArrayList<>();
            Town town = iterator.next();
            if (town.getJwd() != null) {
                map.put("title", town.getTitle());
                map.put("indexId", town.getIndexId());
                map.put("introduced", town.getIntroduce());
                map.put("jwd", town.getJwd());
                map.put("icon_small", getImages(TOWN, "2"));
                Listmap.put("town", town.getTitle());
                mapList1.add(map);
                Listmap.put("List", mapList1);
                mapList.add(Listmap);
            }
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> findEvenByAdyear(Long startyear, Long endyear,String title) throws IOException {
        List<Event> eventList = new LinkedList<>();
        if("".equals(title)||title.isEmpty()){
            eventList = eventRepository.findEvenByAdyear(startyear, endyear);
        }else{
            eventList = eventRepository.findAllByTitle(title);
        }

        Iterator<Event> iterator = eventList.iterator();
        List<Map<String, Object>> listobject = new LinkedList<Map<String, Object>>();

        List<Map<String, Object>> list = new LinkedList<Map<String, Object>>();
        while (iterator.hasNext()) {
            Map<String, Object> object = new HashMap<String, Object>();
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Event event = iterator.next();
            objectMap.put("indexId", event.getIndexId());
            objectMap.put("title", event.getTitle());
            objectMap.put("icon_small", getImages(EVENT, "2"));
            Map<String, Object> object1 = new HashMap<String, Object>();
            List<Town> town = townRepository.findTownByEvent(event.getTitle());
            if (town.size() == 0) {
                continue;
            }
            Iterator<Town> towniterator = town.iterator();
            while (towniterator.hasNext()) {
                Town t = towniterator.next();
                objectMap.put("town", t.getTitle());
                objectMap.put("jwd", t.getJwd());
            }
            list.add(objectMap);
        }
        Map<String, List<Map>> mm = new HashMap();
        int n = 0;
        for (Map temp : list) {
            if (mm.containsKey(temp.get("town"))) {
                mm.get(temp.get("town")).add(temp);
            } else {
                List<Map> ll = new ArrayList<>();
                ll.add(temp);
                mm.put((String) temp.get("town"), ll);
                n = 0;
            }

        }
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();

        for (Map.Entry<String, List<Map>> et : mm.entrySet()) {
            Map<String, Object> res = new HashMap<String, Object>();
            res.put("town", et.getKey());
            res.put("List", et.getValue());
            mapList.add(res);
        }
        return mapList;

    }

    @Override
    public List<Map<String, Object>> findFigureByYear(Long startyear, Long endyear,String title) throws IOException {
        List<Figure> eventList = new LinkedList<>();
        if("".equals(title)||title.isEmpty()){
            eventList = figureRepository.getTown(startyear, endyear);
            List<Figure> eventList1 = figureRepository.getTownStartNull(endyear);
            eventList.addAll(eventList1);
        }else{
            eventList = figureRepository.findAllByTitle(title);
        }

        Iterator<Figure> iterator = eventList.iterator();
        List<Map<String, Object>> list = new LinkedList<Map<String, Object>>();
        while (iterator.hasNext()) {
            Map<String, Object> objectMap = new HashMap<String, Object>();
            Figure figure = iterator.next();
            objectMap.put("indexId", figure.getIndexId());
            objectMap.put("title", figure.getTitle());
            objectMap.put("icon_small", getImages(FIGURE, "2"));
            Map<String, Object> object1 = new HashMap<String, Object>();
            List<Town> town = birthplaceRepository.findTownByFigure(figure.getTitle());
            Iterator<Town> towniterator = town.iterator();
            if (town.size() == 0) {
                continue;
            }
            while (towniterator.hasNext()) {
                Town t = towniterator.next();
                objectMap.put("town", t.getTitle());
                objectMap.put("jwd", t.getJwd());

            }
            list.add(objectMap);
        }
        Map<String, List<Map>> mm = new HashMap();
        int n = 0;
        for (Map temp : list) {
            if (mm.containsKey(temp.get("town"))) {
                mm.get(temp.get("town")).add(temp);
            } else {
                List<Map> ll = new ArrayList<>();
                ll.add(temp);
                mm.put((String) temp.get("town"), ll);
                n = 0;
            }

        }
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();

        for (Map.Entry<String, List<Map>> et : mm.entrySet()) {
            Map<String, Object> res = new HashMap<String, Object>();
            res.put("town", et.getKey());
            res.put("List", et.getValue());
            mapList.add(res);
        }
        return mapList;

    }



    @Override
    public List<Map<String, Object>> getRuinAll(String title) throws IOException {
        List<Ruin> eventList = new LinkedList<>();
        Iterator<Ruin> iterator;
        if("".equals(title)||title.isEmpty()){
            Iterable<Ruin> nodeFirstList = ruinRepository.findAll();
            iterator = nodeFirstList.iterator();
        }else{
            eventList = ruinRepository.findAllByTitle(title);
            iterator = eventList.iterator();
        }

        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        while (iterator.hasNext()) {
            List<Map<String, Object>> mapList1 = new ArrayList<Map<String, Object>>();
            Map<String, Object> Listmap = new HashMap<>();
            Map<String, Object> map = new HashMap<>();
            List<Map> ll = new ArrayList<>();
            Ruin ruin = iterator.next();
            map.put("title", ruin.getTitle());
            map.put("indexId", ruin.getIndexId());
            map.put("introduced", ruin.getIntroduce());
            map.put("jwd", ruin.getJwd());
            map.put("icon_small", getImages(RUIN, "2"));
            map.put("label", ruin.getLabel());
            Listmap.put("town", ruin.getTitle());
            mapList1.add(map);
            Listmap.put("List", mapList1);
            mapList.add(Listmap);
        }
        return mapList;
    }

    public  String getImages(String type, String code) throws IOException {
        String img = "";
        if (DYNASTY.equals(type) && "0".equals(code)) {
            img = "dynastyIcon.png";
        } else if (BIRTHPLACE.equals(type) && "0".equals(code)) {
            img = "placeIcon.png";
        } else if (EVENT.equals(type) && "0".equals(code)) {
            img = "eventIcon.png";
        } else if (OFFICE.equals(type) && "0".equals(code)) {
            img = "officeIcon.png";
        } else if (FIGURE.equals(type) && "0".equals(code)) {
            img = "personIcon.png";
        } else if (TOWN.equals(type) && "0".equals(code)) {
            img = "townIcon.png";
        } else if (RIVER.equals(type) && "0".equals(code)) {
            int randomInt = RandomUtil.randomInt(1, 9);
            if (randomInt == 1) {
                img = "riverIcon1.png";
            } else if (randomInt == 2) {
                img = "riverIcon2.png";
            } else if (randomInt == 3) {
                img = "riverIcon3.png";
            } else if (randomInt == 4) {
                img = "riverIcon4.png";
            } else {
                img = "riverIcon.png";
            }
        } else if (RUIN.equals(type) && "0".equals((code))) {
            img = "historyIcon.png";
        } else if (DYNASTY.equals(type) && "1".equals(code)) {
            img = "朝代icon.png";
        } else if (BIRTHPLACE.equals(type) && "1".equals(code)) {
            img = "籍贯icon.png";
        } else if (EVENT.equals(type) && "1".equals(code)) {
            img = "事件icon.png";
        } else if (OFFICE.equals(type) && "1".equals(code)) {
            img = "官职icon.png";
        } else if (FIGURE.equals(type) && "1".equals(code)) {
            img = "人物icon.png";
        } else if (TOWN.equals(type) && "1".equals(code)) {
            img = "城镇icon.png";
        } else if (RIVER.equals(type) && "1".equals(code)) {
            img = "运河icon.png";
        } else if (RUIN.equals(type) && "1".equals((code))) {
            img = "遗产icon.png";
        } else if (DYNASTY.equals(type) && "2".equals(code)) {
            img = "朝代.png";
        } else if (BIRTHPLACE.equals(type) && "2".equals(code)) {
            img = "籍贯.png";
        } else if (EVENT.equals(type) && "2".equals(code)) {
            img = "事件.png";
        } else if (OFFICE.equals(type) && "2".equals(code)) {
            img = "官职.png";
        } else if (FIGURE.equals(type) && "2".equals(code)) {
            img = "人物.png";
        } else if (TOWN.equals(type) && "2".equals(code)) {
            img = "城镇.png";
        } else if (RIVER.equals(type) && "2".equals(code)) {
            img = "运河.png";
        } else if (RUIN.equals(type) && "2".equals((code))) {
            img = "遗迹.png";
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BufferedImage image = ImageIO.read(new FileInputStream(new File(dir + img)));

        if (image != null) {
            ImageIO.write(image, "png", os);
        }
        InputStream is = new ByteArrayInputStream(os.toByteArray());
        Base64.Encoder encoder = Base64.getEncoder();
        String s = encoder.encodeToString(os.toByteArray());

        return s;
    }



}
